/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.ctrl;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.material.Material;
import com.jme3.math.Matrix4f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.terrain.GeoMap;
import com.jme3.util.BufferUtils;
import com.jme3.scene.Node;
import java.io.IOException;

/**
 *
 * @author JhonKkk
 */
public class SimpleTrailng extends AbstractControl {

    /**
     * @return the isTra
     */
    public boolean isIsTra() {
        return isTra;
    }

    /**
     * @param isTra the isTra to set
     */
    public void setIsTra(boolean isTra) {
        this.isTra = isTra;
    }

    /**
     * @return the material
     */
    public Material getMaterial() {
        return material;
    }

    /**
     * @param material the material to set
     */
    public void setMaterial(Material material) {
        this.material = material;
    }
    //Any local variables should be encapsulated by getters/setters so they
    //appear in the SDK properties window and can be edited.
    //Right-click a local variable to encapsulate it with getters and setters.
    Mesh traMesh;
    Geometry geometry;
    private boolean isF = true;
    private Material material;
    private boolean isTra = false;
    public SimpleTrailng(){
        traMesh = new Mesh();
        traMesh.setDynamic();
        Vector3f v1 = new Vector3f(0, 0, 0);
        Vector3f v2 = new Vector3f(5, 0, 0);
        Vector3f v3 = new Vector3f(5, 5, 0);
        Vector3f v4 = new Vector3f(0, 5, 0);
        int[] indices = new int[]{0, 1, 2, 2, 3, 0};
        traMesh.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(new Vector3f[]{v1, v2, v4, v3}));
        traMesh.setMode(Mesh.Mode.TriangleStrip);
//        traMesh.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(indices));
        traMesh.updateBound();
        geometry = new Geometry("simpleTrailing", traMesh);
    }

    @Override
    protected void controlUpdate(float tpf) {
        //TODO: add code that controls Spatial,
        //e.g. spatial.rotate(tpf,tpf,tpf);
        if(material != null && isF){
            isF = false;
            geometry.setMaterial(material);
            geometry.updateModelBound();
            if(spatial instanceof Node){
                ((Node)spatial).attachChild(geometry);
            }
        }
        if(isTra && material != null){
            Node t = (Node) ((Node)spatial).getChild("t");
            Matrix4f m44 = t.getLocalToWorldMatrix(null);
            Vector3f v = m44.mult(Vector3f.ZERO);
            geometry.setLocalTranslation(v);
        }
    }
    
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        //Only needed for rendering-related operations,
        //not called when spatial is culled.
    }
    
    public Control cloneForSpatial(Spatial spatial) {
        SimpleTrailng control = new SimpleTrailng();
        //TODO: copy parameters to new Control
        return control;
    }
    
    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        InputCapsule in = im.getCapsule(this);
        material = (Material) in.readSavable("material", null);
        isTra = in.readBoolean("isTra", false);
        //TODO: load properties of this Control, e.g.
        //this.value = in.readFloat("name", defaultValue);
    }
    
    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        OutputCapsule out = ex.getCapsule(this);
        out.write(material, "material", null);
        out.write(isTra, "isTra", false);
        //在这里删掉gemetry,否则在编辑器那里会存在多个(因为序列化写spatial时会把spatial的所有东西都写进去,除非你不支持序列化写)
        if(geometry.getParent() != null){
            ((Node)spatial).detachChild(geometry);
        }
        //TODO: save properties of this Control, e.g.
        //out.write(this.value, "name", defaultValue);
    }
    
}
